package dungeonDigger.contentGeneration;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.ShapeRenderer;

import dungeonDigger.Tools.Rectangle;
import dungeonDigger.Tools.References;
import dungeonDigger.entities.GameObject;

public class GameSquare extends GameObject {
	private char				tileLetter	= 'W';
	private int					row, col;
	transient private Room		belongsTo	= null;
	transient private String	directionality;

	public GameSquare(int row, int col) {
		this( 'W', row, col );
	}

	public GameSquare(char c, int row, int col) {
		this.tileLetter = c;
		this.row = row;
		this.col = col;
		this.setPosition( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE );
		int x, y, w, h;
		x = col * DungeonGenerator.DUNGEON_COL_SIZE;
		y = row * DungeonGenerator.DUNGEON_ROW_SIZE;
		w = DungeonGenerator.DUNGEON_COL_SIZE;
		h = DungeonGenerator.DUNGEON_ROW_SIZE;
		switch( this.getTileLetter() ) {
			case 'W':
				break;
			case 'O':
				y = ( row + 1 ) * DungeonGenerator.DUNGEON_ROW_SIZE;
				break;
		}
		this.setCollisionBox( x, y, w, h );
	}

	@Override
	public void update(GameContainer c, int d) {}

	@Override
	public void render(GameContainer c, Graphics g) {
		StringBuffer tileImage = new StringBuffer();
		tileImage.append( References.CURRENT_TILESET );
		tileImage.append( "_" );
		switch( this.getTileLetter() ) {
			case 'W':
				References.IMAGES.get( "WALL1" ).draw( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE );
				if( References.DEBUG_MODE ) {
					g.setColor( Color.blue );
					// ShapeRenderer.draw( new Rectangle( col * DungeonGenerator.ratioCol, row * DungeonGenerator.ratioRow,
					// References.IMAGES.get( "WALL1" ).getWidth(),
					// References.IMAGES.get( "WALL1" ).getHeight() ) );
					g.setColor( Color.yellow );
					ShapeRenderer.draw( this.getCollisionBox() );
					g.setColor( Color.white );
					// ShapeRenderer.draw( this.getDrawBox() );
				}
				break;
			case 'O':
				References.IMAGES.get( "FLOOR1" ).draw( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE );
				if( References.DEBUG_MODE ) {
					g.setColor( Color.blue );
					// ShapeRenderer.draw( new Rectangle( col * DungeonGenerator.ratioCol, row * DungeonGenerator.ratioRow,
					// References.IMAGES.get( "FLOOR1" ).getWidth(),
					// References.IMAGES.get( "FLOOR1" ).getHeight() ) );
					g.setColor( Color.yellow );
					// ShapeRenderer.draw( this.getCollisionBox() ); g.setColor( Color.white );
				}
				break;
			case 'E':
			case 'X':
				References.IMAGES.get( "entranceImage" ).draw( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE );
				break;
		}
		if( References.DEBUG_MODE ) {
			g.scale( .5f, .5f );
			g.drawString( "(" + row + "/" + col + ")", col * DungeonGenerator.DUNGEON_COL_SIZE * 2, row * DungeonGenerator.DUNGEON_ROW_SIZE * 2 );
			g.scale( 2f, 2f );
		}
	}

	@Override
	public float getWidth() {
		return DungeonGenerator.DUNGEON_COL_SIZE;
	}

	@Override
	public float getHeight() {
		return DungeonGenerator.DUNGEON_ROW_SIZE;
	}

	@Override
	public Rectangle getDrawBox() {
		switch( this.getTileLetter() ) {
			case 'W':
				return new Rectangle( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE, References.IMAGES.get( "WALL1" ).getWidth(), References.IMAGES
						.get( "WALL1" ).getHeight() );
			case 'O':
				return new Rectangle( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE, References.IMAGES.get( "FLOOR1" ).getWidth(), References.IMAGES.get(
						"FLOOR1" ).getHeight() );
			case 'E':
			case 'X':
				return new Rectangle( col * DungeonGenerator.DUNGEON_COL_SIZE, row * DungeonGenerator.DUNGEON_ROW_SIZE, References.IMAGES.get( "entranceImage" ).getWidth(), References.IMAGES.get(
						"entranceImage" ).getHeight() );
		}
		return null;
	}

	// //////////////////////
	// GETTERS AND SETTERS //
	// //////////////////////
	public boolean isPassable() {
		return tileLetter != 'W';
	}

	/** @param tileImage the tileImage to set */
	public void setTileLetter(char tileLetter) {
		this.tileLetter = tileLetter;
	}

	/** @return the tileImage */
	public char getTileLetter() {
		return tileLetter;
	}

	/** @return True if the String parameter is the same. */
	public boolean isTileLetter(char s) {
		return tileLetter == s;
	}

	public void setBelongsTo(Room belongsTo) {
		this.belongsTo = belongsTo;
	}

	public Room getBelongsTo() {
		return belongsTo;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public String getDirectionality() {
		return directionality;
	}

	public void setDirectionality(String directionality) {
		this.directionality = directionality;
	}
}
